<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'佣金列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>array(2,3));
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['ordersn']){
		$params['ordersn']=$_RQ['ordersn'];
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('store_commission',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$item['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array(
			'tip'=>'',
			'data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'storetitle','title'=>'门店','type'=>'string'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'money','title'=>'奖励','type'=>'string'),
			array('name'=>'jifen','title'=>'积分','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'type','title'=>'佣金类型','type'=>'radio','options'=>'commission_types'),
			array('name'=>'status','title'=>'佣金状态','type'=>'radio','options'=>'commission_status'),
			array('name'=>'complete_time','title'=>'订单完成时间','type'=>'time'),
			array('name'=>'balancesn','title'=>'结算单号','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'change','title'=>'修改佣金'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'佣金状态','options'=>'commission_status'),
		),
	);
	$return['htmls']=$htmls;
	$return['commission_status']=$this->commission_status;
	$return['commission_types']=$this->commission_types;
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->update('store_commission',array('money'=>$post['money']),array('id'=>$id));
		exi('修改佣金成功！','','list');
	}
	$item=$this->get('store_commission',array('id'=>intval($_RQ['id'])),array('id','money'));
	if(!$item){
		exi('指定佣金不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改佣金',
		'edit'=>array(
			array('type'=>'string','name'=>'money','title'=>'设置佣金','help'=>'佣金异常时操作'),
		),
	);
	exi($return);
}
if($_RQ['op']=='jiesuan'){
	$this->model('store')->create_balance(true);
	exi('一键结算成功！');
}